<template>
	<view class="container">
		<block v-if="!isNext">
			<view class="input">
				<view class="input__text">对方账号：</view>
				<input type="number" placeholder="请输入对方的会员ID" v-model="idValue">
			</view>
			<view class="tips">请在-我的-头像旁边查看会员ID</view>
			<view class="tips">温馨提示：平台禁止津贴/红包有偿转让。</view>
			<view class="btn" @click="handleNextStep">下一步</view>
		</block>
		<block v-if="isNext">
			<view class="user">
				<image class="user__avatar" :src="userList.avatar" />
				<view class="user__info">
					<div class="name">{{userList.nickname}}</div>
					<div class="id">ID：{{userList.uid}}</div>
				</view>
			</view>
			<view class="input">
				<view class="input__text">转让数量：</view>
				<input type="number" placeholder="请输入整数" v-model="numValue">
			</view>
			<view class="tips">我的津贴：{{allowance.member_integral}}</view>
			<view class="btn" @click="handleSubmit">确认转让</view>
		</block>
	</view>
</template>

<script>
	import {
		mapState,
	} from 'vuex';
	export default {
		data() {
			return {
				isNext: false,
				idValue: '',
				numValue: '',
				userList: [],
				allowance: ''
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		mounted() {
			this.getUserAllowance()
		},
		methods: {
			// 获取津贴
			async getUserAllowance() {
				let apiUrl = `/addons/yun_shop/api.php?route=plugin.integral.Frontend.Controllers.Page.index`
				let params = { mid: 140 }
				this.$api.get(apiUrl, params, {paramsType: 2}).then(res => {
					console.log(res,111);
					this.allowance = res.data
				})
			},
			// 点击下一步
			async handleNextStep () {
				if (this.idValue == '') {
					uni.showToast({
						icon: 'error',
						title: 'ID不能为空！'
					})
				} else {
					uni.showLoading({
						title: '提交中',
						mask: true
					});
					let apiUrl = `/addons/yun_shop/api.php?route=plugin.love.Frontend.Controllers.team-dividend-search.index`
					let params = {
						search: this.idValue,
						mid: this.userInfo.uid,
					}
					this.$api.get(apiUrl, params).then(res => {
						wx.hideLoading()
						if (res.result == 1) {
							this.isNext = true;
							this.userList = res.data[0]
						}
					})
				}
			},
			handleSubmit () {
				if (this.numValue == '') {
					uni.showToast({
						icon: 'error',
						title: '数量不能为空！'
					})
					return;
				}
				if (this.numValue.indexOf('.') != -1) {
					uni.showToast({
						icon: 'error',
						title: '请输入整数！'
					})
					return;
				}
				let apiUrl = `/addons/yun_shop/api.php?route=plugin.integral.Frontend.Modules.Integral.Controllers.Transfer`
				let params = {
					mid: this.userInfo.uid,
					recipient_id: this.idValue,
					change_value: this.numValue
				}
				this.$api.get(apiUrl, params).then(res => {
					if (res.result == 1) {
						uni.showToast({
							icon: 'none',
							title: '转让成功！'
						})
						this.getUserAllowance()
						uni.navigateBack({
							delta: 1
						})
					}
				})
			}
		},
	}
</script>

<style lang="scss">
	@import './transfer.scss'
</style>
